package com.fengmoxi.ctv.activity.config.lamp.other;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.fengmoxi.ctv.R;
import com.fengmoxi.ctv.activity.config.lamp.ConfigLampActivity;
import com.fengmoxi.ctv.manage.HomeButtonReceiver;
import com.fengmoxi.ctv.manage.LampManage;

import java.util.Timer;
import java.util.TimerTask;

public class ConfigLampAboutActivity extends AppCompatActivity {
    private static final String TAG = ConfigLampAboutActivity.class.getSimpleName();
    private LampManage mLampManage;

    private TextView mSnTextView;
    private TextView mVersion1TextView;
    private TextView mVersion2TextView;
    private TextView mVersion3TextView;
    private TextView mTemperatureTextView;
    private HomeButtonReceiver mHomeButtonReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_config_lamp_about);

        mSnTextView = findViewById(R.id.sn);
        mVersion1TextView = findViewById(R.id.version1);
        mVersion2TextView = findViewById(R.id.version2);
        mVersion3TextView = findViewById(R.id.version3);
        mTemperatureTextView = findViewById(R.id.temperature);

        mLampManage = new LampManage();

        refresh();

        Timer timer = new Timer();
        timer.schedule(new TimeRefreshTask(), 0, 1000);

        mHomeButtonReceiver = new HomeButtonReceiver(this, new HomeButtonReceiver.OnHomeBtnPressListener() {
            @Override
            public void onHomeBtnPress() {
                if (ConfigLampActivity.configLampActivity != null) {
                    ConfigLampActivity.configLampActivity.finish();
                }
                finish();
            }

            @Override
            public void onHomeBtnLongPress() {

            }
        });
        mHomeButtonReceiver.start();
    }

    @Override
    protected void onDestroy() {
        mHomeButtonReceiver.stop();
        super.onDestroy();
    }

    private class TimeRefreshTask extends TimerTask {
        @Override
        public void run() {
            ConfigLampAboutActivity.this.runOnUiThread(ConfigLampAboutActivity.this::refresh);
        }
    }

    @SuppressLint("SetTextI18n")
    private void refresh() {
        mSnTextView.setText(mLampManage.getSN());
        String[] version = mLampManage.getVersion().split("\\.");
        mVersion1TextView.setText(version[0]);
        mVersion2TextView.setText(version[1]);
        mVersion3TextView.setText(version[2]);
        mTemperatureTextView.setText(mLampManage.getTemperature().split(",")[0] + "℃");
    }
}